#include "bullet_Ground.h"

using std::unique_ptr;

BulletGround::BulletGround(unique_ptr<btDiscreteDynamicsWorld> &rupWorld) : m_rupWorld(rupWorld)
{
	m_upShape.reset(new btStaticPlaneShape(btVector3(0.0f, 1.0f, 0.0f), 1.0f));

	btTransform groundTransform;
	groundTransform.setIdentity();
	groundTransform.setOrigin(btVector3(0.0f, -1.0f, 0.0f));
	m_upMotionState.reset(new btDefaultMotionState(groundTransform));

	btVector3 localInertia;
	m_upShape->calculateLocalInertia(0.0f, localInertia);

	btRigidBody::btRigidBodyConstructionInfo rbInfo(
		0.0f,
		m_upMotionState.get(),
		m_upShape.get(),
		localInertia);

	m_upRigidBody.reset(new btRigidBody(rbInfo));
}

BulletGround::~BulletGround()
{
	if(m_attached) DetachFromWorld();
}

void BulletGround::AttachToWorld()
{
	m_rupWorld->addRigidBody(m_upRigidBody.get());
	m_attached = true;
}

void BulletGround::DetachFromWorld()
{
	m_rupWorld->removeCollisionObject(m_upRigidBody.get());
	m_attached = false;
}